home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / i386-dep.c.notused < prev    next >
Encoding:
Text File  |  1990-11-14  |  32.4 KB  |  1,348 lines

  1. /* Low level interface to ptrace, for GDB when running on the Intel 386.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25.  
  26. #ifdef USG
  27. #include <sys/types.h>
  28. #endif
  29.  
  30. #include <sys/param.h>
  31. #include <sys/dir.h>
  32. #include <signal.h>
  33. #include <sys/user.h>
  34. #include <sys/ioctl.h>
  35. #include <fcntl.h>
  36.  
  37. #ifdef COFF_ENCAPSULATE
  38. #include "a.out.encap.h"
  39. #else
  40. #include <a.out.h>
  41. #endif
  42.  
  43. #include <sys/ptrace.h>
  44.  
  45. #ifndef N_SET_MAGIC
  46. #ifdef COFF_FORMAT
  47. #define N_SET_MAGIC(exec, val) ((exec).magic = (val))
  48. #else
  49. #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
  50. #endif
  51. #endif
  52.  
  53. #include <sys/file.h>
  54. #include <sys/stat.h>
  55.  
  56. #include <sys/reg.h>
  57.  
  58. extern int errno;
  59.  
  60. /* This function simply calls ptrace with the given arguments.  
  61.    It exists so that all calls to ptrace are isolated in this 
  62.    machine-dependent file. */
  63. int
  64. call_ptrace (request, pid, arg3, arg4)
  65.      int request, pid, arg3, arg4;
  66. {
  67.   return ptrace (request, pid, arg3, arg4);
  68. }
  69.  
  70. kill_inferior ()
  71. {
  72.   if (remote_debugging)
  73.     return;
  74.   if (inferior_pid == 0)
  75.     return;
  76.   ptrace (8, inferior_pid, 0, 0);
  77.   wait (0);
  78.   inferior_died ();
  79. }
  80.  
  81. /* This is used when GDB is exiting.  It gives less chance of error.*/
  82.  
  83. kill_inferior_fast ()
  84. {
  85.   if (remote_debugging)
  86.     return;
  87.   if (inferior_pid == 0)
  88.     return;
  89.   ptrace (8, inferior_pid, 0, 0);
  90.   wait (0);
  91. }
  92.  
  93. /* Resume execution of the inferior process.
  94.    If STEP is nonzero, single-step it.
  95.    If SIGNAL is nonzero, give it that signal.  */
  96.  
  97. void
  98. resume (step, signal)
  99.      int step;
  100.      int signal;
  101. {
  102.   errno = 0;
  103.   if (remote_debugging)
  104.     remote_resume (step, signal);
  105.   else
  106.     {
  107.       ptrace (step ? 9 : 7, inferior_pid, 1, signal);
  108.       if (errno)
  109.     perror_with_name ("ptrace");
  110.     }
  111. }
  112.  
  113. #ifdef sprite
  114. #ifdef ATTACH_DETACH
  115.  
  116. /* Start debugging the process whose number is PID.  */
  117.  
  118. attach (pid)
  119.      int pid;
  120. {
  121.   errno = 0;
  122.   ptrace (PTRACE_ATTACH, pid, 0, 0);
  123.   if (errno)
  124.     perror_with_name ("ptrace");
  125.   attach_flag = 1;
  126.   return pid;
  127. }
  128.  
  129. /* Stop debugging the process whose number is PID
  130.    and continue it with signal number SIGNAL.
  131.    SIGNAL = 0 means just continue it.  */
  132.  
  133. void
  134. detach (signal)
  135.      int signal;
  136. {
  137.   errno = 0;
  138.   ptrace (PTRACE_DETACH, inferior_pid, 1, signal);
  139.   if (errno)
  140.     perror_with_name ("ptrace");
  141.   attach_flag = 0;
  142. }
  143. #endif /* ATTACH_DETACH */
  144. #endif
  145.  
  146.  
  147. void
  148. fetch_inferior_registers ()
  149. {
  150.  
  151. #ifdef sprite
  152.   register int regno;
  153.   struct pt_regset inferior_registers;
  154.  
  155.   ptrace (PTRACE_GETREGS, inferior_pid, &inferior_registers);
  156.   for (regno = 0; regno < NUM_REGS; regno++)
  157.     {
  158.       supply_register (regno, &inferior_registers);
  159.     }
  160. #else    
  161.   register int regno;
  162.   register unsigned int regaddr;
  163.   char buf[MAX_REGISTER_RAW_SIZE];
  164.   register int i;
  165.  
  166.   struct user u;
  167.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  168.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  169.  
  170.   for (regno = 0; regno < NUM_REGS; regno++)
  171.     {
  172.       regaddr = register_addr (regno, offset);
  173.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  174.     {
  175.       *(int *) &buf[i] = ptrace (3, inferior_pid, regaddr, 0);
  176.       regaddr += sizeof (int);
  177.     }
  178.       supply_register (regno, buf);
  179.     }
  180. #endif    
  181. }
  182.  
  183. /* Store our register values back into the inferior.
  184.    If REGNO is -1, do this for all registers.
  185.    Otherwise, REGNO specifies which register (so we can save time).  */
  186.  
  187. store_inferior_registers (regno)
  188.      int regno;
  189. {
  190.   register unsigned int regaddr;
  191.   char buf[80];
  192.  
  193. #ifdef sprite
  194.   struct pt_regset inferior_registers;
  195.  
  196.   panic("store_inferior_registers\n");
  197.   ptrace (PTRACE_SETREGS, inferior_pid, &inferior_registers);
  198.  
  199. #else
  200.   struct user u;
  201.   unsigned int offset = (char *) &u.u_ar0 - (char *) &u;
  202.   offset = ptrace (3, inferior_pid, offset, 0) - KERNEL_U_ADDR;
  203.  
  204.   if (regno >= 0)
  205.     {
  206.       regaddr = register_addr (regno, offset);
  207.       errno = 0;
  208.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  209.       if (errno != 0)
  210.     {
  211.       sprintf (buf, "writing register number %d", regno);
  212.       perror_with_name (buf);
  213.     }
  214.     }
  215.   else for (regno = 0; regno < NUM_REGS; regno++)
  216.     {
  217.       regaddr = register_addr (regno, offset);
  218.       errno = 0;
  219.       ptrace (6, inferior_pid, regaddr, read_register (regno));
  220.       if (errno != 0)
  221.     {
  222.       sprintf (buf, "writing register number %d", regno);
  223.       perror_with_name (buf);
  224.     }
  225.     }
  226. #endif    
  227. }
  228.  
  229. /* Copy LEN bytes from inferior's memory starting at MEMADDR
  230.    to debugger memory starting at MYADDR. 
  231.    On failure (cannot read from inferior, usually because address is out
  232.    of bounds) returns the value of errno. */
  233.  
  234. int
  235. read_inferior_memory (memaddr, myaddr, len)
  236.      CORE_ADDR memaddr;
  237.      char *myaddr;
  238.      int len;
  239. {
  240.   register int i;
  241.   /* Round starting address down to longword boundary.  */
  242.   register CORE_ADDR addr = memaddr & - sizeof (int);
  243.   /* Round ending address up; get number of longwords that makes.  */
  244.   register int count
  245.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  246.   /* Allocate buffer of that many longwords.  */
  247.   register int *buffer = (int *) alloca (count * sizeof (int));
  248.   extern int errno;
  249.  
  250.   /* Read all the longwords */
  251.   for (i = 0; i < count; i++, addr += sizeof (int))
  252.     {
  253.       errno = 0;
  254.       if (remote_debugging)
  255.     buffer[i] = remote_fetch_word (addr);
  256.       else
  257.     buffer[i] = ptrace (1, inferior_pid, addr, 0);
  258.       if (errno)
  259.     return errno;
  260.     }
  261.  
  262.   /* Copy appropriate bytes out of the buffer.  */
  263.   bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  264.   return 0;
  265. }
  266.  
  267. /* Copy LEN bytes of data from debugger memory at MYADDR
  268.    to inferior's memory at MEMADDR.
  269.    On failure (cannot write the inferior)
  270.    returns the value of errno.  */
  271.  
  272. int
  273. write_inferior_memory (memaddr, myaddr, len)
  274.      CORE_ADDR memaddr;
  275.      char *myaddr;
  276.      int len;
  277. {
  278.   register int i;
  279.   /* Round starting address down to longword boundary.  */
  280.   register CORE_ADDR addr = memaddr & - sizeof (int);
  281.   /* Round ending address up; get number of longwords that makes.  */
  282.   register int count
  283.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  284.   /* Allocate buffer of that many longwords.  */
  285.   register int *buffer = (int *) alloca (count * sizeof (int));
  286.   extern int errno;
  287.  
  288.   /* Fill start and end extra bytes of buffer with existing memory data.  */
  289.  
  290.   if (remote_debugging)
  291.     buffer[0] = remote_fetch_word (addr);
  292.   else
  293.     buffer[0] = ptrace (1, inferior_pid, addr, 0);
  294.  
  295.   if (count > 1)
  296.     {
  297.       if (remote_debugging)
  298.     buffer[count - 1]
  299.       = remote_fetch_word (addr + (count - 1) * sizeof (int));
  300.       else
  301.     buffer[count - 1]
  302.       = ptrace (1, inferior_pid,
  303.             addr + (count - 1) * sizeof (int), 0);
  304.     }
  305.  
  306.   /* Copy data to be written over corresponding part of buffer */
  307.  
  308.   bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  309.  
  310.   /* Write the entire buffer.  */
  311.  
  312.   for (i = 0; i < count; i++, addr += sizeof (int))
  313.     {
  314.       errno = 0;
  315.       if (remote_debugging)
  316.     remote_store_word (addr, buffer[i]);
  317.       else
  318.     ptrace (4, inferior_pid, addr, buffer[i]);
  319.       if (errno)
  320.     return errno;
  321.     }
  322.  
  323.   return 0;
  324. }
  325.  
  326. /* Work with core dump and executable files, for GDB. 
  327.    This code would be in core.c if it weren't machine-dependent. */
  328.  
  329. #ifndef N_TXTADDR
  330. #define N_TXTADDR(hdr) 0
  331. #endif /* no N_TXTADDR */
  332.  
  333. #ifndef N_DATADDR
  334. #define N_DATADDR(hdr) hdr.a_text
  335. #endif /* no N_DATADDR */
  336.  
  337. /* Make COFF and non-COFF names for things a little more compatible
  338.    to reduce conditionals later.  */
  339.  
  340. #ifndef COFF_FORMAT
  341. #ifndef AOUTHDR
  342. #define AOUTHDR struct exec
  343. #endif
  344. #endif
  345.  
  346. extern char *sys_siglist[];
  347.  
  348.  
  349. /* Hook for `exec_file_command' command to call.  */
  350.  
  351. extern void (*exec_file_display_hook) ();
  352.    
  353. /* File names of core file and executable file.  */
  354.  
  355. extern char *corefile;
  356. extern char *execfile;
  357.  
  358. /* Descriptors on which core file and executable file are open.
  359.    Note that the execchan is closed when an inferior is created
  360.    and reopened if the inferior dies or is killed.  */
  361.  
  362. extern int corechan;
  363. extern int execchan;
  364.  
  365. /* Last modification time of executable file.
  366.    Also used in source.c to compare against mtime of a source file.  */
  367.  
  368. extern int exec_mtime;
  369.  
  370. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  371.  
  372. extern CORE_ADDR data_start;
  373. extern CORE_ADDR data_end;
  374. extern CORE_ADDR stack_start;
  375. extern CORE_ADDR stack_end;
  376.  
  377. /* Virtual addresses of bounds of two areas of memory in the exec file.
  378.    Note that the data area in the exec file is used only when there is no core file.  */
  379.  
  380. extern CORE_ADDR text_start;
  381. extern CORE_ADDR text_end;
  382.  
  383. extern CORE_ADDR exec_data_start;
  384. extern CORE_ADDR exec_data_end;
  385.  
  386. /* Address in executable file of start of text area data.  */
  387.  
  388. extern int text_offset;
  389.  
  390. /* Address in executable file of start of data area data.  */
  391.  
  392. extern int exec_data_offset;
  393.  
  394. /* Address in core file of start of data area data.  */
  395.  
  396. extern int data_offset;
  397.  
  398. /* Address in core file of start of stack area data.  */
  399.  
  400. extern int stack_offset;
  401.  
  402. #ifdef COFF_FORMAT
  403. /* various coff data structures */
  404.  
  405. extern FILHDR file_hdr;
  406. extern SCNHDR text_hdr;
  407. extern SCNHDR data_hdr;
  408.  
  409. #endif /* not COFF_FORMAT */
  410.  
  411. /* a.out header saved in core file.  */
  412.   
  413. extern AOUTHDR core_aouthdr;
  414.  
  415. /* a.out header of exec file.  */
  416.  
  417. extern AOUTHDR exec_aouthdr;
  418.  
  419. extern void validate_files ();
  420.  
  421. core_file_command (filename, from_tty)
  422.      char *filename;
  423.      int from_tty;
  424. {
  425. #ifdef sprite
  426.   panic("core_file_command\n");
  427. #else  
  428.   int val;
  429.   extern char registers[];
  430.  
  431.   /* Discard all vestiges of any previous core file
  432.      and mark data and stack spaces as empty.  */
  433.  
  434.   if (corefile)
  435.     free (corefile);
  436.   corefile = 0;
  437.  
  438.   if (corechan >= 0)
  439.     close (corechan);
  440.   corechan = -1;
  441.  
  442.   data_start = 0;
  443.   data_end = 0;
  444.   stack_start = STACK_END_ADDR;
  445.   stack_end = STACK_END_ADDR;
  446.  
  447.   /* Now, if a new core file was specified, open it and digest it.  */
  448.  
  449.   if (filename)
  450.     {
  451.       filename = tilde_expand (filename);
  452.       make_cleanup (free, filename);
  453.       
  454.       if (have_inferior_p ())
  455.     error ("To look at a core file, you must kill the inferior with \"kill\".");
  456.       corechan = open (filename, O_RDONLY, 0);
  457.       if (corechan < 0)
  458.     perror_with_name (filename);
  459.       /* 4.2-style (and perhaps also sysV-style) core dump file.  */
  460.       {
  461.     struct user u;
  462.  
  463.     int reg_offset;
  464.  
  465.     val = myread (corechan, &u, sizeof u);
  466.     if (val < 0)
  467.       perror_with_name (filename);
  468.     data_start = exec_data_start;
  469.  
  470.     data_end = data_start + NBPG * u.u_dsize;
  471.     stack_start = stack_end - NBPG * u.u_ssize;
  472.     data_offset = NBPG * UPAGES;
  473.     stack_offset = NBPG * (UPAGES + u.u_dsize);
  474.     reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  475.  
  476.     /* I don't know where to find this info.
  477.        So, for now, mark it as not available.  */
  478. /*    N_SET_MAGIC (core_aouthdr, 0);  */
  479.     bzero ((char *) &core_aouthdr, sizeof core_aouthdr);
  480.  
  481.     /* Read the register values out of the core file and store
  482.        them where `read_register' will find them.  */
  483.  
  484.     {
  485.       register int regno;
  486.  
  487.       for (regno = 0; regno < NUM_REGS; regno++)
  488.         {
  489.           char buf[MAX_REGISTER_RAW_SIZE];
  490.  
  491.           val = lseek (corechan, register_addr (regno, reg_offset), 0);
  492.           if (val < 0)
  493.         perror_with_name (filename);
  494.  
  495.            val = myread (corechan, buf, sizeof buf);
  496.           if (val < 0)
  497.         perror_with_name (filename);
  498.           supply_register (regno, buf);
  499.         }
  500.     }
  501.       }
  502.       if (filename[0] == '/')
  503.     corefile = savestring (filename, strlen (filename));
  504.       else
  505.     {
  506.       corefile = concat (current_directory, "/", filename);
  507.     }
  508.  
  509.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  510.                         read_pc ()));
  511.       select_frame (get_current_frame (), 0);
  512.       validate_files ();
  513.     }
  514.   else if (from_tty)
  515.     printf ("No core file now.\n");
  516. #endif  
  517. }
  518.  
  519. exec_file_command (filename, from_tty)
  520.      char *filename;
  521.      int from_tty;
  522. {
  523.   int val;
  524.  
  525.   /* Eliminate all traces of old exec file.
  526.      Mark text segment as empty.  */
  527.  
  528.   if (execfile)
  529.     free (execfile);
  530.   execfile = 0;
  531.   data_start = 0;
  532.   data_end -= exec_data_start;
  533.   text_start = 0;
  534.   text_end = 0;
  535.   exec_data_start = 0;
  536.   exec_data_end = 0;
  537.   if (execchan >= 0)
  538.     close (execchan);
  539.   execchan = -1;
  540.  
  541.   /* Now open and digest the file the user requested, if any.  */
  542.  
  543.   if (filename)
  544.     {
  545.       filename = tilde_expand (filename);
  546.       make_cleanup (free, filename);
  547.       
  548.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  549.             &execfile);
  550.       if (execchan < 0)
  551.     perror_with_name (filename);
  552.  
  553. #ifdef COFF_FORMAT
  554.       {
  555.     int aout_hdrsize;
  556.     int num_sections;
  557.  
  558.     if (read_file_hdr (execchan, &file_hdr) < 0)
  559.       error ("\"%s\": not in executable format.", execfile);
  560.  
  561.     aout_hdrsize = file_hdr.f_opthdr;
  562.     num_sections = file_hdr.f_nscns;
  563.  
  564.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  565.       error ("\"%s\": can't read optional aouthdr", execfile);
  566.  
  567.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  568.                   aout_hdrsize) < 0)
  569.       error ("\"%s\": can't read text section header", execfile);
  570.  
  571.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  572.                   aout_hdrsize) < 0)
  573.       error ("\"%s\": can't read data section header", execfile);
  574.  
  575.     text_start = exec_aouthdr.text_start;
  576.     text_end = text_start + exec_aouthdr.tsize;
  577.     text_offset = text_hdr.s_scnptr;
  578.     exec_data_start = exec_aouthdr.data_start;
  579.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  580.     exec_data_offset = data_hdr.s_scnptr;
  581.     data_start = exec_data_start;
  582.     data_end += exec_data_start;
  583.     exec_mtime = file_hdr.f_timdat;
  584.       }
  585. #else /* not COFF_FORMAT */
  586.       {
  587.     struct stat st_exec;
  588.  
  589. #ifdef HEADER_SEEK_FD
  590.     HEADER_SEEK_FD (execchan);
  591. #endif
  592.  
  593.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  594.  
  595.     if (val < 0)
  596.       perror_with_name (filename);
  597.  
  598.         text_start = N_TXTADDR (exec_aouthdr);
  599.         exec_data_start = N_DATADDR (exec_aouthdr);
  600.  
  601.     text_offset = N_TXTOFF (exec_aouthdr);
  602.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  603.  
  604.     text_end = text_start + exec_aouthdr.a_text;
  605.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  606.     data_start = exec_data_start;
  607.     data_end += exec_data_start;
  608.  
  609.     fstat (execchan, &st_exec);
  610.     exec_mtime = st_exec.st_mtime;
  611.       }
  612. #endif /* not COFF_FORMAT */
  613.  
  614.       validate_files ();
  615.     }
  616.   else if (from_tty)
  617.     printf ("No exec file now.\n");
  618.  
  619.   /* Tell display code (if any) about the changed file name.  */
  620.   if (exec_file_display_hook)
  621.     (*exec_file_display_hook) (filename);
  622. }
  623.  
  624. /* helper functions for m-i386.h */
  625.  
  626. /* stdio style buffering to minimize calls to ptrace */
  627. static CORE_ADDR codestream_next_addr;
  628. static CORE_ADDR codestream_addr;
  629. static unsigned char codestream_buf[sizeof (int)];
  630. static int codestream_off;
  631. static int codestream_cnt;
  632.  
  633. #define codestream_tell() (codestream_addr + codestream_off)
  634. #define codestream_peek() (codestream_cnt == 0 ? \
  635.                codestream_fill(1): codestream_buf[codestream_off])
  636. #define codestream_get() (codestream_cnt-- == 0 ? \
  637.              codestream_fill(0) : codestream_buf[codestream_off++])
  638.  
  639. static unsigned char 
  640. codestream_fill (peek_flag)
  641. {
  642.   codestream_addr = codestream_next_addr;
  643.   codestream_next_addr += sizeof (int);
  644.   codestream_off = 0;
  645.   codestream_cnt = sizeof (int);
  646.   read_memory (codestream_addr,
  647.            (unsigned char *)codestream_buf,
  648.            sizeof (int));
  649.   
  650.   if (peek_flag)
  651.     return (codestream_peek());
  652.   else
  653.     return (codestream_get());
  654. }
  655.  
  656. static void
  657. codestream_seek (place)
  658. {
  659.   codestream_next_addr = place & -sizeof (int);
  660.   codestream_cnt = 0;
  661.   codestream_fill (1);
  662.   while (codestream_tell() != place)
  663.     codestream_get ();
  664. }
  665.  
  666. static void
  667. codestream_read (buf, count)
  668.      unsigned char *buf;
  669. {
  670.   unsigned char *p;
  671.   int i;
  672.   p = buf;
  673.   for (i = 0; i < count; i++)
  674.     *p++ = codestream_get ();
  675. }
  676.  
  677. /* next instruction is a jump, move to target */
  678. static
  679. i386_follow_jump ()
  680. {
  681.   int long_delta;
  682.   short short_delta;
  683.   char byte_delta;
  684.   int data16;
  685.   int pos;
  686.   
  687.   pos = codestream_tell ();
  688.   
  689.   data16 = 0;
  690.   if (codestream_peek () == 0x66)
  691.     {
  692.       codestream_get ();
  693.       data16 = 1;
  694.     }
  695.   
  696.   switch (codestream_get ())
  697.     {
  698.     case 0xe9:
  699.       /* relative jump: if data16 == 0, disp32, else disp16 */
  700.       if (data16)
  701.     {
  702.       codestream_read ((unsigned char *)&short_delta, 2);
  703.       pos += short_delta + 3; /* include size of jmp inst */
  704.     }
  705.       else
  706.     {
  707.       codestream_read ((unsigned char *)&long_delta, 4);
  708.       pos += long_delta + 5;
  709.     }
  710.       break;
  711.     case 0xeb:
  712.       /* relative jump, disp8 (ignore data16) */
  713.       codestream_read ((unsigned char *)&byte_delta, 1);
  714.       pos += byte_delta + 2;
  715.       break;
  716.     }
  717.   codestream_seek (pos + data16);
  718. }
  719.  
  720. /*
  721.  * find & return amound a local space allocated, and advance codestream to
  722.  * first register push (if any)
  723.  *
  724.  * if entry sequence doesn't make sense, return -1, and leave 
  725.  * codestream pointer random
  726.  */
  727. static long
  728. i386_get_frame_setup (pc)
  729. {
  730.   unsigned char op;
  731.   
  732.   codestream_seek (pc);
  733.   
  734.   i386_follow_jump ();
  735.   
  736.   op = codestream_get ();
  737.   
  738.   if (op == 0x58)        /* popl %eax */
  739.     {
  740.       /*
  741.        * this function must start with
  742.        * 
  743.        *    popl %eax          0x58
  744.        *    xchgl %eax, (%esp)  0x87 0x04 0x24
  745.        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
  746.        *
  747.        * (the system 5 compiler puts out the second xchg
  748.        * inst, and the assembler doesn't try to optimize it,
  749.        * so the 'sib' form gets generated)
  750.        * 
  751.        * this sequence is used to get the address of the return
  752.        * buffer for a function that returns a structure
  753.        */
  754.       int pos;
  755.       unsigned char buf[4];
  756.       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
  757.       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
  758.       pos = codestream_tell ();
  759.       codestream_read (buf, 4);
  760.       if (bcmp (buf, proto1, 3) == 0)
  761.     pos += 3;
  762.       else if (bcmp (buf, proto2, 4) == 0)
  763.     pos += 4;
  764.       
  765.       codestream_seek (pos);
  766.       op = codestream_get (); /* update next opcode */
  767.     }
  768.   
  769.   if (op == 0x55)        /* pushl %esp */
  770.     {            
  771.       /* check for movl %esp, %ebp - can be written two ways */
  772.       switch (codestream_get ())
  773.     {
  774.     case 0x8b:
  775.       if (codestream_get () != 0xec)
  776.         return (-1);
  777.       break;
  778.     case 0x89:
  779.       if (codestream_get () != 0xe5)
  780.         return (-1);
  781.       break;
  782.     default:
  783.       return (-1);
  784.     }
  785.       /* check for stack adjustment 
  786.        *
  787.        *  subl $XXX, %esp
  788.        *
  789.        * note: you can't subtract a 16 bit immediate
  790.        * from a 32 bit reg, so we don't have to worry
  791.        * about a data16 prefix 
  792.        */
  793.       op = codestream_peek ();
  794.       if (op == 0x83)
  795.     {
  796.       /* subl with 8 bit immed */
  797.       codestream_get ();
  798.       if (codestream_get () != 0xec)
  799.         return (-1);
  800.       /* subl with signed byte immediate 
  801.        * (though it wouldn't make sense to be negative)
  802.        */
  803.       return (codestream_get());
  804.     }
  805.       else if (op == 0x81)
  806.     {
  807.       /* subl with 32 bit immed */
  808.       int locals;
  809.       codestream_get();
  810.       if (codestream_get () != 0xec)
  811.         return (-1);
  812.       /* subl with 32 bit immediate */
  813.       codestream_read ((unsigned char *)&locals, 4);
  814.       return (locals);
  815.     }
  816.       else
  817.     {
  818.       return (0);
  819.     }
  820.     }
  821.   else if (op == 0xc8)
  822.     {
  823.       /* enter instruction: arg is 16 bit unsigned immed */
  824.       unsigned short slocals;
  825.       codestream_read ((unsigned char *)&slocals, 2);
  826.       codestream_get (); /* flush final byte of enter instruction */
  827.       return (slocals);
  828.     }
  829.   return (-1);
  830. }
  831.  
  832. /* Return number of args passed to a frame.
  833.    Can return -1, meaning no way to tell.  */
  834.  
  835. /* on the 386, the instruction following the call could be:
  836.  *  popl %ecx        -  one arg
  837.  *  addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
  838.  *  anything else    -  zero args
  839.  */
  840.  
  841. int
  842. i386_frame_num_args (fi)
  843.      struct frame_info fi;
  844. {
  845.   int retpc;                        
  846.   unsigned char op;                    
  847.   struct frame_info *pfi;
  848.  
  849.   pfi = get_prev_frame_info ((fi));            
  850.   if (pfi == 0)
  851.     {
  852.       /* Note:  this can happen if we are looking at the frame for
  853.      main, because FRAME_CHAIN_VALID won't let us go into
  854.      start.  If we have debugging symbols, that's not really
  855.      a big deal; it just means it will only show as many arguments
  856.      to main as are declared.  */
  857.       return -1;
  858.     }
  859.   else
  860.     {
  861.       retpc = pfi->pc;                    
  862.       op = read_memory_integer (retpc, 1);            
  863.       if (op == 0x59)                    
  864.     /* pop %ecx */                   
  865.     return 1;                
  866.       else if (op == 0x83)
  867.     {
  868.       op = read_memory_integer (retpc+1, 1);    
  869.       if (op == 0xc4)                
  870.         /* addl $<signed imm 8 bits>, %esp */    
  871.         return (read_memory_integer (retpc+2,1)&0xff)/4;
  872.       else
  873.         return 0;
  874.     }
  875.       else if (op == 0x81)
  876.     { /* add with 32 bit immediate */
  877.       op = read_memory_integer (retpc+1, 1);    
  878.       if (op == 0xc4)                
  879.         /* addl $<imm 32>, %esp */        
  880.         return read_memory_integer (retpc+2, 4) / 4;
  881.       else
  882.         return 0;
  883.     }
  884.       else
  885.     {
  886.       return 0;
  887.     }
  888.     }
  889. }
  890.  
  891. /*
  892.  * parse the first few instructions of the function to see
  893.  * what registers were stored.
  894.  *
  895.  * We handle these cases:
  896.  *
  897.  * The startup sequence can be at the start of the function,
  898.  * or the function can start with a branch to startup code at the end.
  899.  *
  900.  * %ebp can be set up with either the 'enter' instruction, or 
  901.  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
  902.  * but was once used in the sys5 compiler)
  903.  *
  904.  * Local space is allocated just below the saved %ebp by either the
  905.  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
  906.  * a 16 bit unsigned argument for space to allocate, and the
  907.  * 'addl' instruction could have either a signed byte, or
  908.  * 32 bit immediate.
  909.  *
  910.  * Next, the registers used by this function are pushed.  In
  911.  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
  912.  * (and sometimes a harmless bug causes it to also save but not restore %eax);
  913.  * however, the code below is willing to see the pushes in any order,
  914.  * and will handle up to 8 of them.
  915.  *
  916.  * If the setup sequence is at the end of the function, then the
  917.  * next instruction will be a branch back to the start.
  918.  */
  919.  
  920. i386_frame_find_saved_regs (fip, fsrp)
  921.      struct frame_info *fip;
  922.      struct frame_saved_regs *fsrp;
  923. {
  924.   unsigned long locals;
  925.   unsigned char *p;
  926.   unsigned char op;
  927.   CORE_ADDR dummy_bottom;
  928.   CORE_ADDR adr;
  929.   int i;
  930.   
  931.   bzero (fsrp, sizeof *fsrp);
  932.   
  933.   /* if frame is the end of a dummy, compute where the
  934.    * beginning would be
  935.    */
  936.   dummy_bottom = fip->frame - 4 - NUM_REGS*4 - CALL_DUMMY_LENGTH;
  937.   
  938.   /* check if the PC is in the stack, in a dummy frame */
  939.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  940.     {
  941.       /* all regs were saved by push_call_dummy () */
  942.       adr = fip->frame - 4;
  943.       for (i = 0; i < NUM_REGS; i++) 
  944.     {
  945.       fsrp->regs[i] = adr;
  946.       adr -= 4;
  947.     }
  948.       return;
  949.     }
  950.   
  951.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  952.   
  953.   if (locals >= 0) 
  954.     {
  955.       adr = fip->frame - 4 - locals;
  956.       for (i = 0; i < 8; i++) 
  957.     {
  958.       op = codestream_get ();
  959.       if (op < 0x50 || op > 0x57)
  960.         break;
  961.       fsrp->regs[op - 0x50] = adr;
  962.       adr -= 4;
  963.     }
  964.     }
  965.   
  966.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  967.   fsrp->regs[FP_REGNUM] = fip->frame;
  968. }
  969.  
  970. /* return pc of first real instruction */
  971. i386_skip_prologue (pc)
  972. {
  973.   unsigned char op;
  974.   int i;
  975.   
  976.   if (i386_get_frame_setup (pc) < 0)
  977.     return (pc);
  978.   
  979.   /* found valid frame setup - codestream now points to 
  980.    * start of push instructions for saving registers
  981.    */
  982.   
  983.   /* skip over register saves */
  984.   for (i = 0; i < 8; i++)
  985.     {
  986.       op = codestream_peek ();
  987.       /* break if not pushl inst */
  988.       if (op < 0x50 || op > 0x57) 
  989.     break;
  990.       codestream_get ();
  991.     }
  992.   
  993.   i386_follow_jump ();
  994.   
  995.   return (codestream_tell ());
  996. }
  997.  
  998. i386_push_dummy_frame ()
  999. {
  1000.   CORE_ADDR sp = read_register (SP_REGNUM);
  1001.   int regnum;
  1002.   
  1003.   sp = push_word (sp, read_register (PC_REGNUM));
  1004.   sp = push_word (sp, read_register (FP_REGNUM));
  1005.   write_register (FP_REGNUM, sp);
  1006.   for (regnum = 0; regnum < NUM_REGS; regnum++)
  1007.     sp = push_word (sp, read_register (regnum));
  1008.   write_register (SP_REGNUM, sp);
  1009. }
  1010.  
  1011. i386_pop_frame ()
  1012. {
  1013.   FRAME frame = get_current_frame ();
  1014.   CORE_ADDR fp;
  1015.   int regnum;
  1016.   struct frame_saved_regs fsr;
  1017.   struct frame_info *fi;
  1018.   
  1019.   fi = get_frame_info (frame);
  1020.   fp = fi->frame;
  1021.   get_frame_saved_regs (fi, &fsr);
  1022.   for (regnum = 0; regnum < NUM_REGS; regnum++) 
  1023.     {
  1024.       CORE_ADDR adr;
  1025.       adr = fsr.regs[regnum];
  1026.       if (adr)
  1027.     write_register (regnum, read_memory_integer (adr, 4));
  1028.     }
  1029.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  1030.   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
  1031.   write_register (SP_REGNUM, fp + 8);
  1032.   flush_cached_frames ();
  1033.   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  1034.                     read_pc ()));
  1035. }
  1036.  
  1037. /* this table must line up with REGISTER_NAMES in m-i386.h */
  1038. /* symbols like 'EAX' come from <sys/reg.h> */
  1039. static int regmap[] = 
  1040. {
  1041.   EAX, ECX, EDX, EBX,
  1042. #if sprite
  1043.   ESP, EBP, ESI, EDI,
  1044.   EIP, FLAGS, CS, SS,
  1045. /*  DS, ES, FS, GS, */
  1046.   -1, -1, -1, -1
  1047. #else  
  1048.   UESP, EBP, ESI, EDI,
  1049.   EIP, EFL, CS, SS,
  1050.   DS, ES, FS, GS,
  1051. #endif  
  1052. };
  1053.  
  1054. /* blockend is the value of u.u_ar0, and points to the
  1055.  * place where GS is stored
  1056.  */
  1057. i386_register_u_addr (blockend, regnum)
  1058. {
  1059. #if 0
  1060.   /* this will be needed if fp registers are reinstated */
  1061.   /* for now, you can look at them with 'info float'
  1062.    * sys5 wont let you change them with ptrace anyway
  1063.    */
  1064.   if (regnum >= FP0_REGNUM && regnum <= FP7_REGNUM) 
  1065.     {
  1066.       int ubase, fpstate;
  1067.       struct user u;
  1068.       ubase = blockend + 4 * (SS + 1) - KSTKSZ;
  1069.       fpstate = ubase + ((char *)&u.u_fpstate - (char *)&u);
  1070.       return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM));
  1071.     } 
  1072.   else
  1073. #endif
  1074.     return (blockend + 4 * regmap[regnum]);
  1075.   
  1076. }
  1077.  
  1078. i387_to_double (from, to)
  1079.      char *from;
  1080.      char *to;
  1081. {
  1082.   long *lp;
  1083.   /* push extended mode on 387 stack, then pop in double mode
  1084.    *
  1085.    * first, set exception masks so no error is generated -
  1086.    * number will be rounded to inf or 0, if necessary 
  1087.    */
  1088.   asm ("pushl %eax");         /* grab a stack slot */
  1089.   asm ("fstcw (%esp)");        /* get 387 control word */
  1090.   asm ("movl (%esp),%eax");    /* save old value */
  1091.   asm ("orl $0x3f,%eax");        /* mask all exceptions */
  1092.   asm ("pushl %eax");
  1093.   asm ("fldcw (%esp)");        /* load new value into 387 */
  1094.   
  1095.   asm ("movl 8(%ebp),%eax");
  1096.   asm ("fldt (%eax)");        /* push extended number on 387 stack */
  1097.   asm ("fwait");
  1098.   asm ("movl 12(%ebp),%eax");
  1099.   asm ("fstpl (%eax)");        /* pop double */
  1100.   asm ("fwait");
  1101.   
  1102.   asm ("popl %eax");        /* flush modified control word */
  1103.   asm ("fnclex");            /* clear exceptions */
  1104.   asm ("fldcw (%esp)");        /* restore original control word */
  1105.   asm ("popl %eax");        /* flush saved copy */
  1106. }
  1107.  
  1108. double_to_i387 (from, to)
  1109.      char *from;
  1110.      char *to;
  1111. {
  1112.   /* push double mode on 387 stack, then pop in extended mode
  1113.    * no errors are possible because every 64-bit pattern
  1114.    * can be converted to an extended
  1115.    */
  1116.   asm ("movl 8(%ebp),%eax");
  1117.   asm ("fldl (%eax)");
  1118.   asm ("fwait");
  1119.   asm ("movl 12(%ebp),%eax");
  1120.   asm ("fstpt (%eax)");
  1121.   asm ("fwait");
  1122. }
  1123.  
  1124. struct env387 
  1125. {
  1126.   unsigned short control;
  1127.   unsigned short r0;
  1128.   unsigned short status;
  1129.   unsigned short r1;
  1130.   unsigned short tag;
  1131.   unsigned short r2;
  1132.   unsigned long eip;
  1133.   unsigned short code_seg;
  1134.   unsigned short opcode;
  1135.   unsigned long operand;
  1136.   unsigned short operand_seg;
  1137.   unsigned short r3;
  1138.   unsigned char regs[8][10];
  1139. };
  1140.  
  1141. static
  1142. print_387_control_word (control)
  1143. unsigned short control;
  1144. {
  1145.   printf ("control 0x%04x: ", control);
  1146.   printf ("compute to ");
  1147.   switch ((control >> 8) & 3) 
  1148.     {
  1149.     case 0: printf ("24 bits; "); break;
  1150.     case 1: printf ("(bad); "); break;
  1151.     case 2: printf ("53 bits; "); break;
  1152.     case 3: printf ("64 bits; "); break;
  1153.     }
  1154.   printf ("round ");
  1155.   switch ((control >> 10) & 3) 
  1156.     {
  1157.     case 0: printf ("NEAREST; "); break;
  1158.     case 1: printf ("DOWN; "); break;
  1159.     case 2: printf ("UP; "); break;
  1160.     case 3: printf ("CHOP; "); break;
  1161.     }
  1162.   if (control & 0x3f) 
  1163.     {
  1164.       printf ("mask:");
  1165.       if (control & 0x0001) printf (" INVALID");
  1166.       if (control & 0x0002) printf (" DENORM");
  1167.       if (control & 0x0004) printf (" DIVZ");
  1168.       if (control & 0x0008) printf (" OVERF");
  1169.       if (control & 0x0010) printf (" UNDERF");
  1170.       if (control & 0x0020) printf (" LOS");
  1171.       printf (";");
  1172.     }
  1173.   printf ("\n");
  1174.   if (control & 0xe080) printf ("warning: reserved bits on 0x%x\n",
  1175.                 control & 0xe080);
  1176. }
  1177.  
  1178. static
  1179. print_387_status_word (status)
  1180.      unsigned short status;
  1181. {
  1182.   printf ("status 0x%04x: ", status);
  1183.   if (status & 0xff) 
  1184.     {
  1185.       printf ("exceptions:");
  1186.       if (status & 0x0001) printf (" INVALID");
  1187.       if (status & 0x0002) printf (" DENORM");
  1188.       if (status & 0x0004) printf (" DIVZ");
  1189.       if (status & 0x0008) printf (" OVERF");
  1190.       if (status & 0x0010) printf (" UNDERF");
  1191.       if (status & 0x0020) printf (" LOS");
  1192.       if (status & 0x0040) printf (" FPSTACK");
  1193.       printf ("; ");
  1194.     }
  1195.   printf ("flags: %d%d%d%d; ",
  1196.       (status & 0x4000) != 0,
  1197.       (status & 0x0400) != 0,
  1198.       (status & 0x0200) != 0,
  1199.       (status & 0x0100) != 0);
  1200.   
  1201.   printf ("top %d\n", (status >> 11) & 7);
  1202. }
  1203.  
  1204. static
  1205. print_387_status (status, ep)
  1206.      unsigned short status;
  1207.      struct env387 *ep;
  1208. {
  1209.   int i;
  1210.   int bothstatus;
  1211.   int top;
  1212.   int fpreg;
  1213.   unsigned char *p;
  1214.   
  1215.   bothstatus = ((status != 0) && (ep->status != 0));
  1216.   if (status != 0) 
  1217.     {
  1218.       if (bothstatus)
  1219.     printf ("u: ");
  1220.       print_387_status_word (status);
  1221.     }
  1222.   
  1223.   if (ep->status != 0) 
  1224.     {
  1225.       if (bothstatus)
  1226.     printf ("e: ");
  1227.       print_387_status_word (ep->status);
  1228.     }
  1229.   
  1230.   print_387_control_word (ep->control);
  1231.   printf ("last exception: ");
  1232.   printf ("opcode 0x%x; ", ep->opcode);
  1233.   printf ("pc 0x%x:0x%x; ", ep->code_seg, ep->eip);
  1234.   printf ("operand 0x%x:0x%x\n", ep->operand_seg, ep->operand);
  1235.   
  1236.   top = (ep->status >> 11) & 7;
  1237.   
  1238.   printf ("regno  tag  msb              lsb  value\n");
  1239.   for (fpreg = 7; fpreg >= 0; fpreg--) 
  1240.     {
  1241.       double val;
  1242.       
  1243.       printf ("%s %d: ", fpreg == top ? "=>" : "  ", fpreg);
  1244.       
  1245.       switch ((ep->tag >> (fpreg * 2)) & 3) 
  1246.     {
  1247.     case 0: printf ("valid "); break;
  1248.     case 1: printf ("zero  "); break;
  1249.     case 2: printf ("trap  "); break;
  1250.     case 3: printf ("empty "); break;
  1251.     }
  1252.       for (i = 9; i >= 0; i--)
  1253.     printf ("%02x", ep->regs[fpreg][i]);
  1254.       
  1255.       i387_to_double (ep->regs[fpreg], (char *)&val);
  1256.       printf ("  %g\n", val);
  1257.     }
  1258.   if (ep->r0)
  1259.     printf ("warning: reserved0 is 0x%x\n", ep->r0);
  1260.   if (ep->r1)
  1261.     printf ("warning: reserved1 is 0x%x\n", ep->r1);
  1262.   if (ep->r2)
  1263.     printf ("warning: reserved2 is 0x%x\n", ep->r2);
  1264.   if (ep->r3)
  1265.     printf ("warning: reserved3 is 0x%x\n", ep->r3);
  1266. }
  1267.  
  1268. #ifndef U_FPSTATE
  1269. #define U_FPSTATE(u) u.u_fpstate
  1270. #endif
  1271.  
  1272. i386_float_info ()
  1273. {
  1274.  
  1275. #ifdef sprite
  1276.   panic("i386_float_info\n");
  1277. #else  
  1278.   struct user u; /* just for address computations */
  1279.   int i;
  1280.   /* fpstate defined in <sys/user.h> */
  1281.   struct fpstate *fpstatep;
  1282.   char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
  1283.   unsigned int uaddr;
  1284.   char fpvalid;
  1285.   unsigned int rounded_addr;
  1286.   unsigned int rounded_size;
  1287.   extern int corechan;
  1288.   int skip;
  1289.   
  1290.   uaddr = (char *)&u.u_fpvalid - (char *)&u;
  1291.   if (have_inferior_p()) 
  1292.     {
  1293.       unsigned int data;
  1294.       unsigned int mask;
  1295.       
  1296.       rounded_addr = uaddr & -sizeof (int);
  1297.       data = ptrace (3, inferior_pid, rounded_addr, 0);
  1298.       mask = 0xff << ((uaddr - rounded_addr) * 8);
  1299.       
  1300.       fpvalid = ((data & mask) != 0);
  1301.     } 
  1302.   else 
  1303.     {
  1304.       if (lseek (corechan, uaddr, 0) < 0)
  1305.     perror ("seek on core file");
  1306.       if (myread (corechan, &fpvalid, 1) < 0) 
  1307.     perror ("read on core file");
  1308.       
  1309.     }
  1310.   
  1311.   if (fpvalid == 0) 
  1312.     {
  1313.       printf ("no floating point status saved\n");
  1314.       return;
  1315.     }
  1316.   
  1317.   uaddr = (char *)&U_FPSTATE(u) - (char *)&u;
  1318.   if (have_inferior_p ()) 
  1319.     {
  1320.       int *ip;
  1321.       
  1322.       rounded_addr = uaddr & -sizeof (int);
  1323.       rounded_size = (((uaddr + sizeof (struct fpstate)) - uaddr) +
  1324.               sizeof (int) - 1) / sizeof (int);
  1325.       skip = uaddr - rounded_addr;
  1326.       
  1327.       ip = (int *)buf;
  1328.       for (i = 0; i < rounded_size; i++) 
  1329.     {
  1330.       *ip++ = ptrace (3, inferior_pid, rounded_addr, 0);
  1331.       rounded_addr += sizeof (int);
  1332.     }
  1333.     } 
  1334.   else 
  1335.     {
  1336.       if (lseek (corechan, uaddr, 0) < 0)
  1337.     perror_with_name ("seek on core file");
  1338.       if (myread (corechan, buf, sizeof (struct fpstate)) < 0) 
  1339.     perror_with_name ("read from core file");
  1340.       skip = 0;
  1341.     }
  1342.  
  1343.   fpstatep = (struct fpstate *)(buf + skip);
  1344.   print_387_status (fpstatep->status, (struct env387 *)fpstatep->state);
  1345. #endif  
  1346. }
  1347.  
  1348.